package com.xiao.asset.services;


import com.alibaba.fastjson.JSONObject;
import com.xiao.asset.bean.*;
import com.xiao.asset.mapper.AssetMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 衰草寒烟 on 2016/11/7.
 */
@Service("assetservice")
public class AssetService {
    @Autowired
    private AssetMapper assetmapper;
    private static Logger log= Logger.getLogger(AssetService.class);

/**
 * 资产添加
 */
    public String add(AssetBean ab){
        int rs = 0;
        String msg=null;
        try{
            rs= assetmapper.add(ab);
            if(rs>0){
                msg="success";
            }
        }catch (Exception e){
            log.warn("资产添加出错！");
            msg="failure";
        }
        return msg;
    }
    /**
     * 部门添加
     */
    public String bmadd(BumenBean bb){
        int rs = 0;
        String msg=null;
        try{
            rs= assetmapper.bmadd(bb);
            if(rs>0){
                msg="success";
            }
        }catch (Exception e){
            log.warn("部门添加出错！");
            msg="failure";
        }
        return msg;
    }
    /**
     * 类型添加
     */
    public String lxadd(LeixingBean lb){
        int rs = 0;
        String msg=null;
        try{
            rs= assetmapper.lxadd(lb);
            if(rs>0){
                msg="success";
            }
        }catch (Exception e){
            log.warn("类型添加出错！");
            msg="failure";
        }
        return msg;
    }
    /**
     * 业务添加
     */
    public String ywdd(YewuBean yb){
        int rs = 0;
        String msg=null;
        try{
            rs= assetmapper.ywdd(yb);
            if(rs>0){
                msg="success";
            }
        }catch (Exception e){
            log.warn("类型添加出错！");
            msg="failure";
        }
        return msg;
    }
    /**
     * 业务资产添加
     */
    public String yzadd(YewuZichanBean yzb){
        int rs = 0;
        String msg=null;
        try{
            rs= assetmapper.yzadd(yzb);
            if(rs>0){
                msg="success";
            }
        }catch (Exception e){
            log.warn("业务资产添加出错！");
            msg="failure";
        }
        return msg;
    }
    /**
     * 资产删除
     */
    public String del(int assetid){
        int rs=0;
        String msg=null;
        try{
            rs= assetmapper.del(assetid);
            if(rs>0){
                msg="success";
            }
        }catch (Exception e){
            log.warn(assetid+"删除出错！");
            msg="failure";
        }
        return msg;
    }
    /**
     * 部门删除
     */
    public String delbm(int bumenid){
        int rs=0;
        String msg=null;
        try{
            rs= assetmapper.delbm(bumenid);
            if(rs>0){
                msg="success";
            }
        }catch (Exception e){
            log.warn(bumenid+"删除出错！");
            msg="failure";
        }
        return msg;
    }
    /**
     * 类型删除
     */
    public String dellx(int leixingid){
        int rs=0;
        String msg=null;
        try{
            rs= assetmapper.dellx(leixingid);
            if(rs>0){
                msg="success";
            }
        }catch (Exception e){
            log.warn(leixingid+"删除出错！");
            msg="failure";
        }
        return msg;
    }
    /**
     * 业务删除
     */
    public String delyw(int yewuid){
        int rs=0;
        String msg=null;
        try{
            rs= assetmapper.delyw(yewuid);
            if(rs>0){
                msg="success";
            }
        }catch (Exception e){
            log.warn(yewuid+"删除出错！");
            msg="failure";
        }
        return msg;
    }
    /**
     * 获取资产列表
     */
    public List<AssetBean> getlist(){
        List<AssetBean> rs = assetmapper.getlist();
        return rs;
    }
    /**
     * 通过ID获取资产
     */
    public AssetBean getbyid(int assetid){
        AssetBean rs = assetmapper.getbyid(assetid);
        return rs;
    }
    /**
     * 获取某部门的资产列表
     */
    public List<AssetBean> getbmlist(int bumenid){
        List<AssetBean> list=assetmapper.getbmlist(bumenid);
        return list;
    }
    /**
     * 根据类型名称获取类型
     */
    public LeixingBean getlxbyming(String ming){
        LeixingBean rs = assetmapper.getlxbyming(ming);
        return rs;
    }
    /**
     * 根据类型名称获取部门
     */
    public BumenBean getbmbyming(String ming){
        BumenBean rs = assetmapper.getbmbyming(ming);
        return rs;
    }
    /**
     * 根据类型名称获取业务
     */
    public YewuBean getywbyming(String ming){
        YewuBean rs = assetmapper.getywbyming(ming);
        return rs;
    }
    /**
     * 更新资产的使用情况
     */
    public boolean shiyong(int assetidid,String shiyong){
        int rs = assetmapper.shiyong(assetidid,shiyong);
        return rs>0;
    }
    /**
     * 更新资产信息
     */
    public String update(AssetBean ab){
        int rs=0;
        String msg=null;
        try{
            rs = assetmapper.update(ab);
            if(rs>0){
                msg="success";
            }
        }catch (Exception e){
            log.warn(ab.getBianhao()+"修改出错！");
            msg="failure";
        }
        return msg;
    }
    /**
     * 从业务系统中移除某资产（该资产不删除）
     */
    public String ywzcdel(String ywming,String bianhao){
        int rs=0;
        String msg=null;
        try{
            rs = assetmapper.ywzcdel(ywming,bianhao);
            if(rs>0){
                msg="success";
            }
        }catch (Exception e){
            log.warn(ywming+"中移除"+bianhao+"出错！");
            msg="failure";
        }
        return msg;
    }
}
